package cc.booku.owlbox.OrdinaryBooks_norm;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.OrdinaryVideo_norm.OrdinaryVideo_A;
import cc.booku.owlbox.OrdinaryVideo_norm.OrdinaryVideo_C;
import cc.booku.owlbox.OrdinaryVideo_norm.WebVideoActivity;
import cc.booku.owlbox.R;

public class OB_FileScannerActivity extends AppCompatActivity {

    private TextView Fromfileurl, EditClean, EditSave;
    private EditText Filedetails;
    private ListView ListOrdinarybooks;
    private LinearLayout FileEditLayout;
    private Toolbar Toolbar;

    private List<OrdinaryBooks_C> ordinaryBooks_cList = new ArrayList<OrdinaryBooks_C>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ob_file_scanner);

        //加载标题栏
        LoadToolbar();
        //状态栏颜色
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));

        //加载控件
        LoadControl();
        //接收MainActivity传来的数据
        RXDMainActivity();

        //解析json
        //FastJson_analysis();
    }

    private void LoadToolbar() {
        Toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
//        Toolbar.inflateMenu(R.menu.menu_user);
        //设置的点击事件
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });
        //添加menu 菜单点击事件
        Toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_edit:
                        if (FileEditLayout.getVisibility() == View.VISIBLE) {
                            FileEditLayout.setVisibility(View.GONE);
                        } else FileEditLayout.setVisibility(View.VISIBLE);

                        break;

                }
                return true;
            }
        });
    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.menu_norm, menu);//toolbar添加menu菜单
        return true;
    }


    private void LoadControl() {

        Fromfileurl = (TextView) findViewById(R.id.fromfileurl);
        Filedetails = (EditText) findViewById(R.id.filedetails);
        EditClean = (TextView) findViewById(R.id.tv_editclean);
        EditSave = (TextView) findViewById(R.id.tv_editsave);

        ListOrdinarybooks = (ListView) findViewById(R.id.list_ordinarybooks);
        FileEditLayout = (LinearLayout) findViewById(R.id.fileedit_layout);
        //控件监听
        ControlsListen();
    }

    private void ControlsListen() {
        EditClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Filedetails.setText("");
            }
        });
        EditSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Log.d("Fromfileurl", "Fromfileurl.toString()-----" + Fromfileurl.getText().toString());
                //重新写入数据到文件
                if (FileoOperations.fileIsExists(Fromfileurl.getText().toString())) {
                    //先输入空字符串，清空文件
                    //FileoOperations.writeAssignTxt(Fromfileurl.getText().toString(), "");
                    // Log.d("Filedetails", "Filedetails.toString()" + Filedetails.getText().toString());
                    //写入修改后的字符
                    FileoOperations.writeAssignTxt(Fromfileurl.getText().toString(), Filedetails.getText().toString());
                    //隐藏
                    FileEditLayout.setVisibility(View.GONE);
                    //等待刷新
                    WaitingRefresh();
                } else
                    Toast.makeText(OB_FileScannerActivity.this, "编辑失败", Toast.LENGTH_SHORT).show();

            }
        });

    }

    //等待刷新函数
    private void WaitingRefresh() {
        Toast.makeText(OB_FileScannerActivity.this, "编辑成功", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // finish();
                ordinaryBooks_cList.clear();
                RXDMainActivity();
            }
        }, 2000);
        // onCreate(null);//调用此方法刷新页面

    }

    private void RXDMainActivity() {

        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        String SpecifiedFile = bundle.getString("SpecifiedFile");

        Fromfileurl.setText(SpecifiedFile);

        //读取的文件数据：FileoOperations.readFromSDFile(this,SpecifiedFile)
        Filedetails.setText(FileoOperations.readFromSDFile(this, SpecifiedFile));

        try {
            //尝试解析代码
            FastJson_analysis(SpecifiedFile);
        } catch (Exception e) {
            //异常处理
            Toast.makeText(OB_FileScannerActivity.this, "数据解析错误", Toast.LENGTH_SHORT).show();
        }

    }

    private OrdinaryBooks_C ordinaryBooksC;
    private OrdinaryBooks_A ordinaryBooksA;

    //fastjson解析json--来自aliyun的开源库
    private void FastJson_analysis(String str) {
//指定文件内容转换为SpecifiedFile------string类型
        String SpecifiedFile = FileoOperations.readFromSDFile(this, str);
//使用fastJson解析Json
        List<OrdinaryBooks_C> ordinaryBooksCList = JSON.parseArray(SpecifiedFile.toString(), OrdinaryBooks_C.class);
        for (OrdinaryBooks_C normBook : ordinaryBooksCList) {
            ordinaryBooksC = new OrdinaryBooks_C();

            /*
             * vod_id,vod_name,vod_author,vod_class,vod_add_time,vod_content
             *
             * */

            String vod_id = normBook.getVod_id();
            String vod_name = normBook.getVod_name();
            String vod_author = normBook.getVod_author();
            String vod_class = normBook.getVod_class();
            String vod_add_time = normBook.getVod_add_time();
            String vod_content = normBook.getVod_content();


            ordinaryBooksC.setVod_id(vod_id);
            ordinaryBooksC.setVod_name(vod_name);
            ordinaryBooksC.setVod_author(vod_author);
            ordinaryBooksC.setVod_class(vod_class);
            ordinaryBooksC.setVod_add_time(vod_add_time);
            ordinaryBooksC.setVod_content(vod_content);

            ordinaryBooks_cList.add(ordinaryBooksC);


        }
        Message msg = new Message();
        msg.what = 2;
        handler.sendMessage(msg);//用activity中的handler发送消息

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {


                case 2:
                    listClickListener((ArrayList<OrdinaryBooks_C>) ordinaryBooks_cList);
                    break;


            }
        }
    };

    public void listClickListener(ArrayList<OrdinaryBooks_C> ordinaryBooks_cList) {


        ordinaryBooksA = new OrdinaryBooks_A(R.layout.ordinarybooks_item, this, ordinaryBooks_cList);

        ListOrdinarybooks.setAdapter(ordinaryBooksA);
        ListOrdinarybooks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //Toast.makeText(getContext(), "Click ++ item" + i, Toast.LENGTH_SHORT).show();

                final TextView vod_id1 = (TextView) view.findViewById(R.id.vod_id);
                final TextView vod_name1 = (TextView) view.findViewById(R.id.vod_name);
                final TextView vod_author1 = (TextView) view.findViewById(R.id.vod_author);
                final TextView vod_class1 = (TextView) view.findViewById(R.id.vod_class);
                final TextView vod_add_time1 = (TextView) view.findViewById(R.id.vod_add_time);
                final TextView vod_content1 = (TextView) view.findViewById(R.id.vod_content);


                String vod_id = vod_id1.getText().toString();
                String vod_name = vod_name1.getText().toString();
                String vod_author = vod_author1.getText().toString();
                String vod_class = vod_class1.getText().toString();
                String vod_add_time = vod_add_time1.getText().toString();
                String vod_content = vod_content1.getText().toString();

                Intent intent = new Intent(OB_FileScannerActivity.this, PreViewActivity.class);

                //用Bundle携带数据
                Bundle bundle = new Bundle();
                //传递name参数为specifiedFile
                bundle.putString("vod_name", vod_name);
                bundle.putString("vod_content", vod_content);
                intent.putExtras(bundle);
                startActivity(intent);

//                Intent intent = new Intent();
//                Bundle bundle = new Bundle();
//                bundle.putString("vod_class", vod_class2);
//                bundle.putString("vod_score", vod_score2);
//                bundle.putString("vod_name", vod_name2);
//                bundle.putString("vod_add_time", vod_add_time2);
//                bundle.putString("vod_play_url", vod_play_url2);
//                bundle.putString("vod_value_add", vod_value_add2);
//                bundle.putString("vod_pic", vod_pic_url2);
//                intent.putExtras(bundle);
//                intent.setClass(getActivity(), PlayerActivity.class);
//                startActivity(intent);


            }
        });
    }


}
